import Dexie from 'dexie';
//sql: 'create table if not exists messages("msgId" varchar(40) PRIMARY Key,"groupId" varchar(40),"categoryId" varchar(20),"sendUserid" varchar(50),"atUserid" varchar(50),"toUserid" varchar(50),"toUsername" nvarchar(50),"sendTime" varchar(50),"readTime" varchar(50),"msgState" varchar(10),"msgAction" varchar(20),"msgClass" varchar(20),"sendContent" TEXT(1000),"sendObject" TEXT(100) ,"sendUsername" nvarchar(50),"fp" varchar(100) ,"headimgurl" varchar(150),"type" varchar(20))',
//sql: 'create table if not exists groupMessageState("groupId" varchar(40) PRIMARY Key,"groupName" nvarchar(50),"unreadMessagesCount" INT,"toUserid" varchar(50), "toUsername" nvarchar(50),"sendUserid" varchar(50), "sendUsername" nvarchar(50),"categoryId" varchar(20),"branchId" varchar(50),"headimgurl" varchar(100),"isPrichat" CHAR(1),"status" char(1),"sendTime" varchar(50),"loadTime" datetime)',
//messages:'&msgId,groupId,categoryId,sendUserid,atUserid,toUserid,toUsername,sendTime,readTime,msgState,msgAction,msgClass,sendContent,sendObject,sendUsername,fp,headimgurl,type',

//下面只是定义索引，实际上可以放任何字段到表里面。不需要索引的字段不建议定义在下面。
export class ImDataBaseH5 { 
    constructor(userid) {
        var imDataBase = new Dexie('imDataBase_'+userid);
        imDataBase.version(2).stores({
            messages: '&msgId,groupId',
            groupMessageState:'&groupId,categoryId,branchId,isPrichat,gstatus,guStatus',
            config:'++id,prichatGroupTime,groupTime,groupMessageTime,groupUserTime,userFrendTime',
            groupUser:'&id,groupId,userid,username,status,headimgurl,lvl,joinTime,allowSpeechTime,inviteTime',
            userFrend:'&frendUserid,phoneno',
        });
        imDataBase.config.reverse().first(item=>{ 
           // that.loadImDataFromServer(item); 
            console.log("imDataBase.config.reverse().first()---->>"+item);
            if(!item){
                imDataBase.config.add({prichatGroupTime:null,prichatMessageTime:null,groupTime:null,groupMessageTime:null,groupUserTime:null,userFrendTime:null});
            }
        }).catch(e=>{
            console.log("imDataBase.config.reverse().first()---->>异常");
            console.error(e);
        });
        this.db=imDataBase;
    }
} 